package com.quiputechnology.nexwm.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.quiputechnology.nexwm.model.Usuario;
import com.quiputechnology.nexwm.service.LoginException;
import com.quiputechnology.nexwm.service.LoginManager;

public class LoginAction extends DispatchAction {

	private LoginManager loginManager;

	public ActionForward login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LoginForm loginForm = (LoginForm) form; // Binding del request

		try {
			Usuario usuario = loginManager.login(loginForm.getUsername(),
					loginForm.getPassword());
			HttpSession session = request.getSession();
			session.setAttribute("usuario", usuario);
			return mapping.findForward("bienvenida");
		} catch (LoginException e) {
			request.setAttribute("mensaje", e.getMessage());
			return mapping.findForward("login");
		}
	}

	public void setLoginManager(LoginManager loginManager) {
		this.loginManager = loginManager;
	}
}